<?php

class Model_Block {

    private static $DISTANCE = 20; //  2 km

    public function getAllBlock() {
        $sql = "Select * from Block";
        $db = Model_Connections::getInstance()->getDbConnect();
        $data = $db->fetchAll($sql);
        return $data;
    }

    public function api_getBlock($page, $count) {

        if ($page == null || $count == null) {
            return null;
        } else {
            $db = Model_Connections::getInstance()->getDbConnect();
            $page = mysql_real_escape_string($page);
            $count = mysql_real_escape_string($count);
            
            $sql = "Select * from Block ";
            $start = $count * ($page - 1);
            $end = $page * $count;
            $sql .= ' LIMIT ' . $start . ',' . $end;
            $data = $db->fetchAll($sql);
            if ($data != null && !empty($data)) {
                return $data;
            } else {
                return null;
            }
        }
    }

    public function getBlockId($lon, $lat) {
        if ($lon == null || $lon == "" || $lat == null || $lat == "")
            return null;
        else {
            $db = Model_Connections::getInstance()->getDbConnect();
            $blockId = -1;
            $queryBlock = 'select *
                           from Block b
                           where (acos(sin(b.lat) * sin(' . $lat . ') + cos(b.lat) * cos(' . $lat . ') * cos(b.lon- ' . $lon . ')) * 6371) < ' . self::$DISTANCE;
            $data = $db->fetchAll($queryBlock);

            if ($data != null && !empty($data)) {
                $blockId = $data[0]['id'];
            } else {
                $queryBlock = 'select *
                           from Block b
                           where (acos(sin(b.lat) * sin(' . $lat . ') + cos(b.lat) * cos(' . $lat . ') * cos(b.lon- ' . $lon . ')) * 6371) < ' . self::$DISTANCE * 2;
                $data = $db->fetchAll($queryBlock);
                if ($data != null && !empty($data)) {
                    $blockId = $data[0]['id'];
                } else {
                    $blockId = -1;
                }
            }
            return $blockId;
        }
        return null;
    }

    public function insertNewBlock($lon, $lat, $description) {
        if ($lon == null || $lon == "" || $lat == null || $lat == "")
            return false;
        else {
            if ($description == null)
                $description = "";
            $db = Model_Connections::getInstance()->getDbConnect();
            return $db->query("Insert into Block(lon,lat,description) values('$lon','$lat',N'$description')");
        }
    }

    public function deleteBlock($BlockId) {
        if ($BlockId == null || $BlockId == "")
            return false;
        else {
            $db = Model_Connections::getInstance()->getDbConnect();
            return $db->query("DELETE FROM Block WHERE id = '$BlockId'");
        }
    }

    public function updateBlock($id, $lon, $lat, $description) {
        if ($id == null || $id == "" || $lon == null || $lon == "" || $lat == null || $lat == "")
            return false;
        else {
            if ($description == null)
                $description = "";
            $db = Model_Connections::getInstance()->getDbConnect();
            return $db->query("UPDATE Block SET lon='" . $lon . "', lat='" . $lat . "', description=N'" . $description . "     ' WHERE id='" . $id . "'");
        }
    }

}

?>
